package com.ldgk.pim.client.view.gwt;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ldgk.pim.client.presenter.PlacePresenter;
import com.ldgk.pim.client.presenter.Presenter;
import com.ldgk.pim.client.view.PlaceView;
import com.ldgk.pim.data.Place;

public class PlaceViewNativeGWTImpl implements PlaceView {
	private Window window = new Window();
	private Grid controlGrid = new Grid(1, 4);

	// Form Widgets
	private TextBox name = new TextBox();
	private TextBox address = new TextBox();
	private TextBox email = new TextBox();
	private TextBox phones = new TextBox();
	private TextBox contact = new TextBox();

	private RadioButton hospitalRadio = new RadioButton("type", "Hospital  ");
	private RadioButton schoolRadio = new RadioButton("type", "Escola");

	private Button ok = new Button("OK");
	private Button cancel = new Button("Cancelar");
	private Button distributions = new Button("Distribui&ccedil;oes");
	private Button remove = new Button("Excluir");

	// Current showing distribution place
	private Place place;

	// Presenter
	private PlacePresenter presenter;

	/**
	 * Default constructor, builds the form and initialize the appropriate
	 * listeners
	 */
	public PlaceViewNativeGWTImpl() {
		controlGrid.setWidget(0, 0, ok);
		controlGrid.setWidget(0, 1, cancel);
		controlGrid.setWidget(0, 3, remove);
		controlGrid.setWidget(0, 2, distributions);

		window.setSize(360, 230);
		window.setPlain(true);
		window.setModal(true);
		window.setBlinkModal(true);
		window.setHeading("Local de Distribui&ccedil;ao");
		window.setLayout(new FitLayout());

		VerticalPanel pnl = new VerticalPanel();
		pnl.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		pnl.add(this.buildForm());
		pnl.add(controlGrid);

		window.add(pnl, new FitData(1));
		this.attachListeners();
	}

	/**
	 * Create or edit a distribution place
	 * 
	 * @param p
	 *            - DistributionPlace
	 */
	public void editPlace(Place p) {
		this.place = p;
		if (p.getKey() != -1) {
			// Already existent distribution place
			distributions.setVisible(true);
			remove.setVisible(true);
		} else {
			// New distribution place, gotta to save it first in order to
			// add distribution events
			distributions.setVisible(false);
			remove.setVisible(false);
		}
		placeToForm(p);
		window.show();
	}

	/**
	 * {@inheritDoc}
	 */
	public void setPresenter(Presenter presenter) {
		this.presenter = (PlacePresenter) presenter;
	}

	private void attachListeners() {
		cancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				window.setVisible(false);
			}
		});

		ok.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				window.setVisible(false);
				formToPlace(place);
				presenter.savePlace(place);
			}
		});

		remove.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				MessageBox.confirm("Confirmacao", "Deseja realmente excluir este local e as distribuicoes associadas a ele?",
						new Listener<MessageBoxEvent>() {
							public void handleEvent(MessageBoxEvent ce) {
								if (ce.getButtonClicked().getText().equals("Yes")) {
									window.setVisible(false);
									presenter.deletePlace(place);
								}
							}
						});
			}
		});
	}

	private void formToPlace(Place p) {
		p.setName(name.getText());
		p.setAddress(address.getText());
		p.setEmail(email.getText());
		p.setPhones(phones.getText());
		p.setContactPerson(contact.getText());
		if (hospitalRadio.getValue()) {
			p.setType(Place.HOSPITAL);
		} else if (schoolRadio.getValue()) {
			p.setType(Place.SCHOOL);
		}

	}

	private void placeToForm(Place p) {
		name.setText(p.getName());
		address.setText(p.getAddress());
		email.setText(p.getEmail());
		phones.setText(p.getPhones());
		contact.setText(p.getContactPerson());

		if (p.getType() == Place.HOSPITAL) {
			hospitalRadio.setValue(true);
		} else if (p.getType() == Place.SCHOOL) {
			schoolRadio.setValue(true);

		}
	}

	private Grid buildForm() {
		String boxSize = "260px";
		name.setWidth(boxSize);
		address.setWidth(boxSize);
		email.setWidth(boxSize);
		phones.setWidth(boxSize);
		contact.setWidth(boxSize);

		Grid grid = new Grid(6, 2);
		grid.setWidget(0, 0, new Label("Nome:"));
		grid.setWidget(0, 1, name);

		grid.setWidget(1, 0, new Label("Endereco:"));
		grid.setWidget(1, 1, address);

		grid.setWidget(2, 0, new Label("E-mail:"));
		grid.setWidget(2, 1, email);

		grid.setWidget(3, 0, new Label("Telefones:"));
		grid.setWidget(3, 1, phones);

		grid.setWidget(4, 0, new Label("Contato:"));
		grid.setWidget(4, 1, contact);

		grid.setWidget(5, 0, new Label("Tipo:"));
		FlowPanel typePanel = new FlowPanel();
		typePanel.add(hospitalRadio);
		typePanel.add(schoolRadio);
		grid.setWidget(5, 1, typePanel);

		return grid;
	}
}
